﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Transactions;

namespace CompanyDBInterface
{
    class CompanyDAL
    {
        #region ADD
        public void AddCompany(Company company)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Companies.AddObject(company);
                context.SaveChanges();
            }
        }

        public void AddEmployee(Employee emp)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Employees.AddObject(emp);
                context.SaveChanges();
            }
        }

        public void AddPerson(Person person)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.People.AddObject(person);
                context.SaveChanges();
            }
        }

        public void AddCountry(Country country)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Countries.AddObject(country);
                context.SaveChanges();
            }
        }

        public void AddCustomer(CompaniesCustomer compCustomer)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.CompaniesCustomers.AddObject(compCustomer);
                context.SaveChanges();
            }
        }
        #endregion
        #region Update
        public void EditCompany(Company company)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Companies.Attach(company);

                context.ObjectStateManager.GetObjectStateEntry(company).SetModified();
                context.Refresh(RefreshMode.ClientWins, company);
                context.SaveChanges();  
            }
        }

        public void EditEmployee(Employee emp)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Employees.Attach(emp);
                context.ObjectStateManager.GetObjectStateEntry(emp).SetModified();
                context.Refresh(RefreshMode.ClientWins, emp);
                context.SaveChanges();
            }
        }

        public void EditPerson(Person person)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.People.Attach(person);

                context.ObjectStateManager.GetObjectStateEntry(person).SetModified();
                context.Refresh(RefreshMode.ClientWins, person);
                context.SaveChanges();
            }
        }

        public void EditCountry(Country country)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Countries.Attach(country);

                context.ObjectStateManager.GetObjectStateEntry(country).SetModified();
                context.Refresh(RefreshMode.ClientWins, country);
                context.SaveChanges();
            }
        }

        public void EditCompanyCustomer(CompaniesCustomer companyCustomer)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.CompaniesCustomers.Attach(companyCustomer);

                context.ObjectStateManager.GetObjectStateEntry(companyCustomer).SetModified();
                context.Refresh(RefreshMode.ClientWins, companyCustomer);
                context.SaveChanges();
            }
        }
        #endregion
        #region Delete
        public void DeleteCompany(Company company)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Companies.DeleteObject(company);
                context.SaveChanges();
            }
        }

        public void DeleteEmployee(Employee emp)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Employees.DeleteObject(emp);
                context.SaveChanges();
            }
        }

        public void DeletePerson(Person person)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.People.DeleteObject(person);
                context.SaveChanges();
            }
        }

        public void DeleteCountry(Country country)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.Countries.DeleteObject(country);
                context.SaveChanges();
            }
        }

        public void DeleteCompanyCustomer(CompaniesCustomer companyCustomer)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            using (context)
            {
                context.CompaniesCustomers.DeleteObject(companyCustomer);
                context.SaveChanges();
            }
        }
        #endregion
        #region Read
        public Company GetCompanyById(int id)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            Company company;
            using (context)
            {
                company = (from c in context.Companies
                                  where c.ID == id
                                  select c).First();
            }
            return company;
        }

        public CompaniesCustomer GetCompanyCustomerById(int id)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            CompaniesCustomer companyCustomer;
            using (context)
            {
                companyCustomer = (from c in context.CompaniesCustomers
                           where c.ID == id
                           select c).First();
            }
            return companyCustomer;
        }

        public Employee GetEmployeeById(int id)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            Employee employee;
            using (context)
            {
                employee = (from c in context.Employees
                           where c.ID == id
                           select c).First();
            }
            return employee;
        }

        public Person GetPersonById(int id)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            Person person;
            using (context)
            {
                person = (from c in context.People
                           where c.ID == id
                           select c).First();
            }
            return person;
        }

        public Country GetCountryById(int id)
        {
            CompanyDBEntities context = new CompanyDBEntities();
            Country country;
            using (context)
            {
                country = (from c in context.Countries
                           where c.ID == id
                           select c).First();
            }
            return country;
        }
        #endregion

        public void FirePeople()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                CompanyDBEntities context = new CompanyDBEntities();
                using (context)
                {
                    DateTime fireDate = new DateTime(2010,1,1);
                    var employeesToFire = from e in context.Employees
                                          where e.HireDate > fireDate
                                          select e;
                    int employeesCount = context.Employees.Select(e => e).Count();
                    if (employeesCount/2 > employeesToFire.Count())
                    {
                        List<Employee> empsToFire = employeesToFire.ToList();
                        foreach (Employee emp in empsToFire)
                        {
                            context.Employees.DeleteObject(emp);
                            context.SaveChanges();
                        }
                    }
                }
                scope.Complete();
            }
        }
    }
}
